package com.sikaryofficial.workbench.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.system.api.domain.SysUser;
import com.sikaryofficial.system.api.model.FileInfo;
import com.sikaryofficial.system.api.model.outer.OrderPayInfoResp;
import com.sikaryofficial.system.api.model.outer.UploadPaymentVoucherReq;
import com.sikaryofficial.workbench.domain.dto.req.order.GoodsOrderReq;
import com.sikaryofficial.workbench.domain.dto.req.order.OrderItemReq;
import com.sikaryofficial.workbench.domain.dto.req.order.OrderListReq;
import com.sikaryofficial.workbench.domain.dto.req.order.OrderSaveReq;
import com.sikaryofficial.workbench.domain.dto.resp.order.GoodsOrderDTO;
import com.sikaryofficial.workbench.domain.dto.resp.order.OrderCustomizedDTO;
import com.sikaryofficial.workbench.domain.dto.resp.order.OrderDTO;
import com.sikaryofficial.workbench.domain.dto.resp.order.ShoppingCartDTO;
import com.sikaryofficial.workbench.manager.OrderManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * 订单Controller
 *
 * @author wuweihong
 * @date 2024-09-23
 */
@RestController
@RequestMapping("/order")
@Api(tags = "渠道商-订单模块")
public class OrderController {

	@Autowired
	private OrderManager orderManager;

	@ApiOperation("工作台-商品加入购物车")
	@PostMapping("/addGoodsToCart")
	public R<ShoppingCartDTO> addToCart(@RequestBody GoodsOrderReq goodsOrderReq) {
		return R.ok(orderManager.addGoodsToCart(goodsOrderReq));
	}

	@ApiOperation("工作台-获取用户购物车")
	@GetMapping("/getCart")
	public R<ShoppingCartDTO> getCart() {
		return R.ok(orderManager.getShoppingCart());
	}

	@ApiOperation("工作台-查询订单列表")
	@PostMapping("/listPage")
	public PageResult<OrderDTO> orderListPage(@Validated @RequestBody OrderListReq orderListReq) {
		IPage<OrderDTO> page = orderManager.orderListPage(orderListReq);
		return PageUtil.success(page.getRecords(), page.getTotal());
	}

	@ApiOperation("工作台-调整指定订单的数量")
	@PostMapping("/updateOrderItem")
	public R<Void> updateOrderItem(@Validated @RequestBody OrderItemReq req) {
		orderManager.updateOrderItemInCart(req);
		return R.ok();
	}

	@ApiOperation("工作台-订单询价")
	@PostMapping("/getQuote")
	public R<SysUser> getQuote() {
		return R.ok(orderManager.getQuote());
	}

	@GetMapping("/getCustomerContractUrl/{orderHeaderId}")
	@ApiOperation("工作台-获取合同签署、查看地址")
	public R<String> getCustomerContractUrl(@PathVariable("orderHeaderId") Long orderHeaderId) {
		return orderManager.getCustomerContractUrl(orderHeaderId);
	}

	/**
	 * 获取支付信息
	 *
	 * @param orderHeaderId
	 * @param payType
	 * @return
	 */
	@GetMapping("/getPayInfo/{orderHeaderId}")
	@ApiOperation("获取支付信息")
	public R<OrderPayInfoResp> getPayInfo(@ApiParam("订单id") @PathVariable("orderHeaderId") Long orderHeaderId,
	                                      @ApiParam(value = "支付类型 1-首付款 2-尾款") @RequestParam Integer payType) {
		return orderManager.getPayInfo(orderHeaderId, payType);
	}

	/**
	 * 上传支付凭证
	 *
	 * @param req
	 * @return
	 */
	@ApiOperation("上传支付凭证")
	@PostMapping("/uploadPaymentVoucher")
	public R<Boolean> uploadPaymentVoucher(@RequestBody @Validated UploadPaymentVoucherReq req) {
		return orderManager.uploadPaymentVoucher(req);
	}

	/**
	 * 获取发货单文件
	 *
	 * @param orderHeaderId
	 * @return
	 */
	@ApiOperation("获取发货单文件")
	@GetMapping("/getShipmentFile")
	public R<List<FileInfo>> getShipmentFile(@RequestParam("orderHeaderId") Long orderHeaderId) {
		return R.ok(orderManager.getShipmentFile(orderHeaderId));
	}

	/**
	 * 文件上传至内网obs
	 *
	 * @param file
	 * @return
	 */
	@ApiOperation("文件上传至内网obs")
	@PostMapping("/uploadAttachment")
	public R<String> uploadAttachment(@RequestParam("file") MultipartFile file) throws IOException {
		return orderManager.uploadAttachment(file);
	}

	@ApiOperation("工作台-删除购物车中的商品")
	@PostMapping("/remove/{goodsId}")
	public R<ShoppingCartDTO> removeGoods(@PathVariable("goodsId") Long goodsId) {
		return R.ok(orderManager.removeGoods(goodsId));
	}

	@ApiOperation("工作台-计算商品价格和折扣")
	@PostMapping("/calc/goodsAmount")
	public R<GoodsOrderDTO> calcGoodsAmount(@RequestBody GoodsOrderReq req) {
		return R.ok(orderManager.calcGoodsAmount(req));
	}

	@ApiOperation("工作台-根据商品数量进行调价")
	@GetMapping("/getGoodsPriceByQuantity/{goodsId}/{quantity}")
	public R<BigDecimal> getGoodsPriceByQuantity(@PathVariable("goodsId") Long goodsId, @PathVariable("quantity") Integer quantity) {
		return R.ok(orderManager.getGoodsPriceByQuantity(goodsId, quantity));
	}


	@ApiOperation("工作台-查询定制详情")
	@PostMapping("/customize")
	public R<List<OrderCustomizedDTO>> getOrderCustomize(@RequestParam("orderHeaderId") Long orderHeaderId) {
		return R.ok(orderManager.getOrderCustomizedById(orderHeaderId));
	}

	@ApiOperation("工作台-拒绝定制单")
	@PostMapping("/orderCustomize/reject")
	public R<Void> rejectOrderItemCustomize(@RequestBody OrderSaveReq req) {
		orderManager.rejectOrderItemCustomized(req);
		return R.ok();
	}

	@ApiOperation("工作台-确认定制单")
	@PostMapping("/orderCustomize/confirm")
	public R<Void> confirmCustomize(@RequestParam("orderHeaderId") Long orderHeaderId) {
		orderManager.confirmOrderCustomized(orderHeaderId);
		return R.ok();
	}
}
